package com.kfpanda.search.index;

//import static org.quartz.CronScheduleBuilder.cronSchedule;
//import static org.quartz.TriggerBuilder.newTrigger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.bind.JAXBException;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
//import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Repository;

import com.kfpanda.search.index.context.IndexBean;
import com.kfpanda.search.index.context.IndexOXMFactory;
import com.kfpanda.search.index.context.IndexResource;
import com.kfpanda.search.index.context.ResourceBean;

/**
 * <b>ClassName:<b> IndexsCreate <br>
 * <b>Description: <br>
 * 开启定时任务，创建索引。
 * 
 * @Author: kfpanda
 * @Date：2012-2-5 下午09:14:33
 * @ModifyUser：kfpanda
 * 
 */
@Repository("indexCreate")
public class IndexCreate {

	private Logger logger = Logger.getLogger(IndexCreate.class);

/*	@Resource
	private IndexOXMFactory indexOXMFactory;
	private IndexResource indexResource;

	public void init() {
		try {
			indexResource = indexOXMFactory.loadIndexResource();
		} catch (IOException e) {
			logger.error("初始化index.xml配置文件出错，文件读取失败。", e);
		} catch (JAXBException e) {
			logger.error("初始化index.xml配置文件出错，castor文件解组失败。", e);
		}
	}
	
	public void indexCreateBatch(){
		this.init();
		if(this.indexResource == null || this.indexResource.getIndexs() == null){
			return ;
		}
		List<IndexBean> indexList = this.indexResource.getIndexs();
		List<Scheduler> schedList = new ArrayList<Scheduler>();
		for(int i = 0; i < indexList.size(); i++){
			try {
				schedList.add( createIndexScheduler(indexList.get(i)) );
			} catch (SchedulerException e) {
				logger.error("配置quartz定时任务失败。", e);
			}
		}
		
		try {
			startScheduler(schedList);
		} catch (SchedulerException e) {
			logger.error("开始quartz定时任务失败，", e);
		}
	}
	
	public void startScheduler(List<Scheduler> schedList) throws SchedulerException{
		for(int i = 0, len = schedList.size(); i < len; i++){
			schedList.get(i).start();
		}
	}
	
	public Scheduler createIndexScheduler(IndexBean indexBean) throws SchedulerException{
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();
		String indexId = indexBean.getId();
		
		List<ResourceBean> resourceList = indexBean.getResources();
		for(int i = 0, len = resourceList.size(); i < len; i++){
			ResourceBean source = resourceList.get(i);
			String cronExpression = StringUtils.isBlank(source.getCronExpression())
								? indexBean.getCronExpression() : source.getCronExpression();
			String suffId = "_" + indexId + "_" + source.getId();
			JobDataMap dataMap = new JobDataMap();
			dataMap.put("indexBean", indexBean);
			dataMap.put("sourceBean", source);
			JobDetail jobDetail = JobBuilder.newJob(IndexJob.class)
						.withIdentity("job" + suffId, "group" + suffId)
						.usingJobData(dataMap).build();
			CronTrigger trigger = newTrigger()
			            .withIdentity("trigger" + suffId , "group" + suffId)
			            .withSchedule(cronSchedule(cronExpression))
			            .build();
			sched.scheduleJob(jobDetail, trigger);
		}
		return sched;
	}
*/
}
